{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "51978669-a414-4641-b66b-0474d8e1691e",
   "metadata": {},
   "source": [
    "# 第七节、索引切片和迭代"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "823d4959-912e-4f2a-b7fa-a9bfffeca440",
   "metadata": {},
   "source": [
    "numpy中的数组切片是原始数组的视图，这意味着数据不会被复制，视图上任何数据的修改都会反映到原数组上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e84d3285-e856-4ebb-b498-e4ebfe800dc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "691bcb2d-b1ad-45b6-ae9a-829718d47bfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.50783016, 0.6861437 , 0.5451591 , 0.12315701, 0.32840989],\n",
       "       [0.16922578, 0.07304445, 0.65372356, 0.4864595 , 0.01619846],\n",
       "       [0.893906  , 0.6827188 , 0.73056561, 0.08059825, 0.36262918],\n",
       "       [0.33125299, 0.50427215, 0.90687001, 0.3496748 , 0.18334493]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.random(size=(4, 5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78da7e23-53a1-4a66-886a-3166e8a525b8",
   "metadata": {},
   "source": [
    "一维数组就不说了，和list大差不差，下面说二维和高维的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04a69987-4674-46ab-a724-d95b49eed1fb",
   "metadata": {},
   "source": [
    "## 索引单个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "97545ad4-3b8e-4249-b06f-5d7d94b4ca70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.16922578226560958"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1, 0]  # 第二行第一列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a288429-000a-4647-bbf1-084c4bb865a7",
   "metadata": {},
   "source": [
    "## 索引某一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7f62239b-cd3b-4f06-8f17-cee018446acd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.16922578, 0.07304445, 0.65372356, 0.4864595 , 0.01619846])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1]    # 这是简写方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a8eb5388-f719-4854-9c52-a521e40a0af4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.16922578, 0.07304445, 0.65372356, 0.4864595 , 0.01619846])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1,:]  # 更全的写法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfe5d70b-850e-4f8c-b1d5-488c448139b2",
   "metadata": {},
   "source": [
    "## 索引某一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d006d74e-5fe7-4eb4-b9c2-ef6ba8d6473a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.50783016, 0.16922578, 0.893906  , 0.33125299])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:, 0]  # 取出第1列，索引列的时候不能省略行："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45ebeee8-1fe4-4a89-a3b7-041460222bcc",
   "metadata": {},
   "source": [
    "## 索引某一行或一列返回结果保持原来的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4bd0db0f-343a-4d11-9284-bb4dd521ab22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.16922578, 0.07304445, 0.65372356, 0.4864595 , 0.01619846]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[[1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9bf39f6c-c1c7-4d83-9699-b6d814190ee5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.5451591 ],\n",
       "       [0.65372356],\n",
       "       [0.73056561],\n",
       "       [0.90687001]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:, [2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b50c6963-6901-417b-8564-915669f65fae",
   "metadata": {},
   "source": [
    "## 切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "391514b6-30cc-4584-a2f4-865658791d7c",
   "metadata": {},
   "source": [
    "切片和List差不多[start: stop: step]其中[start,stop)左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f512eb02-f94d-4655-9158-23c4d056af74",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对切片的赋值每个元素，与列表操作不同\n",
    "arr[:,2] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c671e2b5-a771-433f-8cba-fe44a61c4d7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.50783016, 0.6861437 , 0.        , 0.12315701, 0.32840989],\n",
       "       [0.16922578, 0.07304445, 0.        , 0.4864595 , 0.01619846],\n",
       "       [0.893906  , 0.6827188 , 0.        , 0.08059825, 0.36262918],\n",
       "       [0.33125299, 0.50427215, 0.        , 0.3496748 , 0.18334493]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d093b4f8-a1b4-4d85-b17e-8e91d74bc2e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr[1, 2] = 999"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ba5166cc-41a9-45e9-be40-4e04592b9f2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.07830161e-01, 6.86143699e-01, 0.00000000e+00, 1.23157007e-01,\n",
       "        3.28409887e-01],\n",
       "       [1.69225782e-01, 7.30444513e-02, 9.99000000e+02, 4.86459500e-01,\n",
       "        1.61984644e-02],\n",
       "       [8.93906002e-01, 6.82718803e-01, 0.00000000e+00, 8.05982516e-02,\n",
       "        3.62629183e-01],\n",
       "       [3.31252992e-01, 5.04272155e-01, 0.00000000e+00, 3.49674796e-01,\n",
       "        1.83344930e-01]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b6f27285-7c3e-468c-a33f-cd3105ba7480",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.32840989, 0.12315701, 0.        , 0.6861437 ])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0, -1:0:-1]   # 第1行，列从最后一列开始往前切，切到第2列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dac3f8a2-6211-4a41-b1f2-897d0b2cf813",
   "metadata": {},
   "source": [
    "## 花式索引和索引技巧"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d12dbeac-ea94-47d0-b325-b80ee26d9a13",
   "metadata": {},
   "source": [
    "整数数组进行索引即花式索引，其和切片不一样，它总是将数据复制到新数组中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "cd3361bc-b475-45ce-bee8-b4283eb81fd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(1, 11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ff0df025-4f6b-474c-a12d-409b1ae723b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d35e6686-d2fb-4eaa-a759-129c5482cbe1",
   "metadata": {},
   "source": [
    "花式索引之打两个中括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "005409a8-9f15-4098-889f-ea603427e73b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# arr2是arr1的花式索引结果\n",
    "arr2 = arr1[[1, 3, 3, 5, 7, 7, 7]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "239965b3-0dc0-40cc-96e7-b782969be3fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 4, 6, 8, 8, 8])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "25fa86dd-aabf-48f2-bfdd-a1ba5208a605",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 改变arr2的最后一个元素\n",
    "arr2[-1] = 999"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "2a15d66b-4c85-41c2-878e-6bb8979a0933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  2,   4,   4,   6,   8,   8, 999])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ef0fb53b-67ce-4eb6-a75f-eb6c35239487",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1  # arr1并没被改变，说明arr2是深拷贝的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd092696-c023-4539-8622-8ff481a5037d",
   "metadata": {},
   "source": [
    "花式索引之布尔值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "33556761-f678-4f3d-93d7-42d23bb4b943",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "f711c188-6d08-43b1-8636-7e0e76765e99",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个布尔条件\n",
    "condition = arr1 > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "155db54f-695c-446a-9095-055f2fb8debd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False,  True,  True,  True,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "aab62d30-0a18-4ed2-83b2-ae371f81e3d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把这个布尔条件数组作为索引传入\n",
    "arr1[condition]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e416c98e-7fa1-45fa-a97f-7f26ae5ad9fd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
